package cn.ts.android.attendance.data;

import cn.ts.android.attendance.lock.ObjWithLock;
import cn.ts.android.attendance.model.Attendence;

import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class AttenDanceDataHelperWithLock extends ObjWithLock<AttenDanceDataHelper> {

    private static final long serialVersionUID = 8549668315606224029L;

    /**
     */
    public AttenDanceDataHelperWithLock(AttenDanceDataHelper dataHelper) {
        super(dataHelper);
    }

    /**
     * @param lock
     */
    public AttenDanceDataHelperWithLock(AttenDanceDataHelper dataHelper, ReentrantReadWriteLock lock) {
        super(dataHelper, lock);
    }


    /**
     *
     * @return
     */
    public List<Attendence> get() {
        ReentrantReadWriteLock.ReadLock readLock = this.getLock().readLock();
        readLock.lock();
        try {
            AttenDanceDataHelper dataHelper = this.getObj();
            return dataHelper.getAttendacne();
        } catch (Throwable e) {
            //log.error(e.getMessage(), e);
        } finally {
            readLock.unlock();
        }
        return null;
    }


    /**
     *
     */
    public void putAttendance(List<Attendence> attendences) {
        ReentrantReadWriteLock.WriteLock writeLock = this.getLock().writeLock();
        writeLock.lock();
        try {
            AttenDanceDataHelper dataHelper = this.getObj();
            dataHelper.putAttendacne(attendences);
        } finally {
            writeLock.unlock();
        }
    }


    /**
     *
     */
    public void update(List<Attendence> attendences) {
        ReentrantReadWriteLock.WriteLock writeLock = this.getLock().writeLock();
        writeLock.lock();
        try {
            AttenDanceDataHelper dataHelper = this.getObj();
            dataHelper.updateAttendacne(attendences);
        } finally {
            writeLock.unlock();
        }
    }
}
